package com.novoda.runbuddy.activity;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import com.novoda.runbuddy.R;
import com.novoda.runbuddy.activity.stopwatch.RemoteStopClockProxy;
import com.novoda.runbuddy.controller.DayControllerImpl;
import com.novoda.runbuddy.controller.IDayController;
import com.novoda.runbuddy.controller.IOptionsMenu;
import com.novoda.runbuddy.controller.OptionsMenuImpl;
import com.novoda.runbuddy.model.ListOfRuns;
import com.novoda.runbuddy.view.RunListAdapter;

public class Today extends ListActivity {

	private IDayController	dayOfRunDetailsController;
	private IOptionsMenu	optionsMenu;
	private ListOfRuns		allRunsToday;
	public static final int	MAPOFJOG	= 1;

	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.today);
		optionsMenu = new OptionsMenuImpl(this);
		dayOfRunDetailsController = new DayControllerImpl(this);
		allRunsToday = dayOfRunDetailsController.getTodaysRuns();

		showLastRunDetailsAndTotals(allRunsToday);
		listAllRunsToday(allRunsToday);
	}

	private void showLastRunDetailsAndTotals(ListOfRuns allRunsToday) {

		dayOfRunDetailsController.createTodayView(this, (TextView) findViewById(R.id.miles),
				(TextView) findViewById(R.id.day_total_time), allRunsToday);
	}

	private void listAllRunsToday(ListOfRuns runEntries) {

		if (runEntries.size() > 0) {
			listAllTodaysRunDetails(runEntries);
		} else {
			runEntries = new ListOfRuns();
			listAllTodaysRunDetails(runEntries);
		}

	}

	private void listAllTodaysRunDetails(ListOfRuns runEntries) {
		setListAdapter(new RunListAdapter(this, runEntries, dayOfRunDetailsController));
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		optionsMenu.addAllActivitiesToMenu(menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		optionsMenu.selectItem(item);
		return super.onOptionsItemSelected(item);
	}

	@Override
	protected void onStop() {
		super.onStop();
		RemoteStopClockProxy.getInstance().unregisterListener(this);
	}

	@Override
	protected void onStart() {
		super.onStart();
		RemoteStopClockProxy.getInstance().registerListener(this);
	}

}
